

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.shell.oper &mdash; Cup 3.2.19 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> Cup
          

          
          </a>

          
            
            
              <div class="version">
                3.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cup.html">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>cup.shell.oper</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.shell.oper</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Zhao Minghao, Guannan Ma</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    shell operations related module</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">print_function</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">uuid</span>
<span class="kn">import</span> <span class="nn">tempfile</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">import</span> <span class="nn">signal</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">hashlib</span>
<span class="kn">import</span> <span class="nn">platform</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">subprocess</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">err</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">platforms</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">decorators</span>


<span class="c1"># linux only import</span>
<span class="k">if</span> <span class="n">platform</span><span class="o">.</span><span class="n">system</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;Linux&#39;</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">cup.res</span> <span class="k">import</span> <span class="n">linux</span>
    <span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;rm&#39;</span><span class="p">,</span> <span class="s1">&#39;rmrf&#39;</span><span class="p">,</span> <span class="s1">&#39;kill&#39;</span><span class="p">,</span>
        <span class="s1">&#39;is_process_used_port&#39;</span><span class="p">,</span> <span class="s1">&#39;is_port_used&#39;</span><span class="p">,</span> <span class="s1">&#39;is_proc_exist&#39;</span><span class="p">,</span>
        <span class="s1">&#39;is_proc_exist&#39;</span><span class="p">,</span> <span class="s1">&#39;is_process_running&#39;</span><span class="p">,</span>
        <span class="s1">&#39;contains_file&#39;</span><span class="p">,</span> <span class="s1">&#39;backup_file&#39;</span><span class="p">,</span>
        <span class="s1">&#39;ShellExec&#39;</span>
    <span class="p">]</span>
<span class="c1"># universal import (platform indepedent)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;contains_file&#39;</span><span class="p">,</span> <span class="s1">&#39;backup_file&#39;</span>
    <span class="p">]</span>


<span class="c1"># linux functionalities {{</span>

<span class="c1"># pylint: disable=C0103</span>
<span class="k">def</span> <span class="nf">rm</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    rm the file if no exception happens.</span>
<span class="sd">    Will not raise exception if it fails</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
        <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;rm oserror: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">error</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">rmrf</span><span class="p">(</span><span class="n">fpath</span><span class="p">,</span> <span class="n">safemode</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :param fpath:</span>
<span class="sd">        files/direcotry to be deleted.</span>
<span class="sd">    :param safemode:</span>
<span class="sd">        True by default. You cannot delete root / when safemode is True</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needlinux</span>
    <span class="k">def</span> <span class="nf">_real_rmrf</span><span class="p">(</span><span class="n">fpath</span><span class="p">,</span> <span class="n">safemode</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        real rmrf</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">safemode</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">fpath</span><span class="p">))</span> <span class="o">==</span> <span class="s1">&#39;/&#39;</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">ShellException</span><span class="p">(</span><span class="s1">&#39;cannot rmtree root / under safemode&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">fpath</span><span class="p">):</span>
            <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">fpath</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="n">fpath</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">_real_rmrf</span><span class="p">(</span><span class="n">fpath</span><span class="p">,</span> <span class="n">safemode</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">is_process_running</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Judge if the executable is running by comparing /proc files.</span>
<span class="sd">    :platforms:</span>
<span class="sd">        linux only. Will raise exception if running on other platforms</span>
<span class="sd">    :param path:</span>
<span class="sd">        executable current working direcotry</span>
<span class="sd">    :param name:</span>
<span class="sd">        executable name</span>
<span class="sd">    :return:</span>
<span class="sd">        return True if the process is running. Return False otherwise.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needlinux</span>
    <span class="k">def</span> <span class="nf">_real_is_proc_exist</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        _real_is_proc_exist</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;ps -ef|grep </span><span class="si">%s</span><span class="s1">|grep -v &quot;^grep &quot;|grep -v &quot;^vim &quot;|grep -v &quot;^less &quot;|</span><span class="se">\</span>
<span class="s1">            grep -v &quot;^vi &quot;|grep -v &quot;^cat &quot;|grep -v &quot;^more &quot;|grep -v &quot;^tail &quot;|</span><span class="se">\</span>
<span class="s1">            awk </span><span class="se">\&#39;</span><span class="s1">{print $2}</span><span class="se">\&#39;</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
        <span class="n">pids</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">pids</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">pids</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">pids</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">pids</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">sel_path</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;cwd&quot;</span><span class="p">,</span> <span class="s2">&quot;exe&quot;</span><span class="p">]:</span>
                <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;ls -l /proc/</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">|awk </span><span class="se">\&#39;</span><span class="s1">{print $11}</span><span class="se">\&#39;</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pid</span><span class="p">,</span> <span class="n">sel_path</span><span class="p">)</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
                <span class="n">pid_path</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">pid_path</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="c1"># print(&#39;%s is exist: %s&#39; % (name, path))</span>
                    <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="k">return</span> <span class="n">_real_is_proc_exist</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>


<span class="c1"># for compatibility. Do not delete this line:</span>
<span class="n">is_proc_exist</span> <span class="o">=</span> <span class="n">is_process_running</span>


<span class="k">def</span> <span class="nf">_kill_child</span><span class="p">(</span><span class="n">pid</span><span class="p">,</span> <span class="n">sign</span><span class="p">):</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;ps -ef|grep </span><span class="si">%s</span><span class="s1">|grep -v grep|awk </span><span class="se">\&#39;</span><span class="s1">{print $2,$3}</span><span class="se">\&#39;</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pid</span><span class="p">)</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
    <span class="n">pids</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">proc</span> <span class="ow">in</span> <span class="n">pids</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">proc</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">p_id</span> <span class="o">=</span> <span class="n">proc</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">p_id</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">pid</span><span class="p">:</span>
            <span class="n">_kill_child</span><span class="p">(</span><span class="n">p_id</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sign</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">p_id</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">pid</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sign</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="s1">&#39;kill </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pid</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;9&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;-9&#39;</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="s1">&#39;kill -9 </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pid</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;SIGSTOP&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;19&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;-19&#39;</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="s1">&#39;kill -19 </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pid</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;SIGCONT&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;18&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;-18&#39;</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="s1">&#39;kill -18 </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;sign error&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">kill</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">sign</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">b_kill_child</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    will judge if the process is running by calling function</span>
<span class="sd">    (is_process_running), then send kill signal to this process</span>
<span class="sd">    :param path:</span>
<span class="sd">        executable current working direcotry (cwd)</span>
<span class="sd">    :param name:</span>
<span class="sd">        executable name</span>
<span class="sd">    :param sign:</span>
<span class="sd">        kill sign, e.g. 9 for SIGKILL, 15 for SIGTERM</span>
<span class="sd">    :b_kill_child:</span>
<span class="sd">        kill child processes or not. False by default.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
    <span class="c1"># path = os.path.abspath(path)</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;ps -ef|grep </span><span class="si">%s</span><span class="s1">|grep -v grep|awk </span><span class="se">\&#39;</span><span class="s1">{print $2}</span><span class="se">\&#39;</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">)</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
    <span class="n">pids</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">pids</span><span class="p">:</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;ls -l /proc/</span><span class="si">%s</span><span class="s1">/cwd|awk </span><span class="se">\&#39;</span><span class="s1">{print $11}</span><span class="se">\&#39;</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pid</span><span class="p">)</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">pid_path</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">pid_path</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">path</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">pid_path</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">b_kill_child</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
                <span class="n">_kill_child</span><span class="p">(</span><span class="n">pid</span><span class="p">,</span> <span class="n">sign</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sign</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="s1">&#39;kill </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pid</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;9&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;-9&#39;</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="s1">&#39;kill -9 </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pid</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;SIGSTOP&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;19&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;-19&#39;</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="s1">&#39;kill -19 </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pid</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;SIGCONT&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;18&#39;</span> <span class="ow">or</span> <span class="n">sign</span> <span class="o">==</span> <span class="s1">&#39;-18&#39;</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">execshell</span><span class="p">(</span><span class="s1">&#39;kill -18 </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;sign error&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">True</span>


<div class="viewcode-block" id="backup_file"><a class="viewcode-back" href="../../../cup.shell.html#cup.shell.oper.backup_file">[docs]</a><span class="k">def</span> <span class="nf">backup_file</span><span class="p">(</span><span class="n">srcpath</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">dstpath</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Backup srcpath/filename to dstpath/filenamne.label.</span>
<span class="sd">    If label is None, cup will use time.strftime(&#39;%H:%M:S&#39;)</span>

<span class="sd">    :dstpath:</span>
<span class="sd">        will create the folder if no existence</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">label</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%H:%M:%S&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">dstpath</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">dstpath</span><span class="p">)</span>
    <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span>
        <span class="n">srcpath</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">filename</span><span class="p">,</span> <span class="n">dstpath</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">filename</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">label</span>
    <span class="p">)</span></div>


<span class="k">def</span> <span class="nf">backup_folder</span><span class="p">(</span><span class="n">srcpath</span><span class="p">,</span> <span class="n">foldername</span><span class="p">,</span> <span class="n">dstpath</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    same to backup_file except it&#39;s a FOLDER not a FILE.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">label</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%H:%M:%S&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">dstpath</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">dstpath</span><span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span>
        <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">srcpath</span><span class="p">,</span> <span class="n">foldername</span><span class="p">),</span>
        <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">dstpath</span><span class="p">,</span> <span class="n">foldername</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">label</span><span class="p">)</span>
    <span class="p">)</span>


<span class="k">def</span> <span class="nf">is_path_contain_file</span><span class="p">(</span><span class="n">dstpath</span><span class="p">,</span> <span class="n">dstfile</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">follow_link</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    use contains_file instead. Kept still for compatibility purpose</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">contains_file</span><span class="p">(</span><span class="n">dstpath</span><span class="p">,</span> <span class="n">dstfile</span><span class="p">,</span> <span class="n">recursive</span><span class="p">,</span> <span class="n">follow_link</span><span class="p">)</span>


<div class="viewcode-block" id="contains_file"><a class="viewcode-back" href="../../../cup.shell.html#cup.shell.oper.contains_file">[docs]</a><span class="k">def</span> <span class="nf">contains_file</span><span class="p">(</span><span class="n">dstpath</span><span class="p">,</span> <span class="n">expected_name</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">follow_link</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    judge if the dstfile is in dstpath</span>

<span class="sd">    :param dstpath:</span>
<span class="sd">        search path</span>
<span class="sd">    :param dstfile:</span>
<span class="sd">        file</span>
<span class="sd">    :param recursive:</span>
<span class="sd">        search recursively or not. False by default.</span>
<span class="sd">    :return:</span>
<span class="sd">        return True on success, False otherwise</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">dstpath</span><span class="p">)</span>
    <span class="n">fpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">expected_name</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
    <span class="n">fullpath</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{0}</span><span class="s1">/</span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">expected_name</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
    <span class="n">fullpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">fullpath</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">recursive</span><span class="p">:</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">__</span><span class="p">,</span> <span class="n">fnames</span><span class="p">)</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">followlinks</span><span class="o">=</span><span class="n">follow_link</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">fnames</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">filename</span> <span class="o">==</span> <span class="n">fpath</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span></div>


<span class="k">def</span> <span class="nf">is_port_used</span><span class="p">(</span><span class="n">port</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    judge if the port is used or not (It&#39;s not 100% sure as next second, some</span>
<span class="sd">    other process may steal the port as soon after this function returns)</span>

<span class="sd">    :platform:</span>
<span class="sd">        linux only (netstat command used inside)</span>
<span class="sd">    :param port:</span>
<span class="sd">        expected port</span>
<span class="sd">    :return:</span>
<span class="sd">        return True if the port is used, False otherwise</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needlinux</span>
    <span class="k">def</span> <span class="nf">__is_port_used</span><span class="p">(</span><span class="n">port</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;internal func&quot;&quot;&quot;</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;netstat -nl | grep &#39;:</span><span class="si">%s</span><span class="s2"> &#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">port</span><span class="p">)</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
        <span class="k">if</span> <span class="mi">0</span> <span class="o">!=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">stdout</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="mi">0</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">stdout</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="n">__is_port_used</span><span class="p">(</span><span class="n">port</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">is_process_used_port</span><span class="p">(</span><span class="n">process_path</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    judge if a process is using the port</span>

<span class="sd">    :param process_path:</span>
<span class="sd">        process current working direcotry (cwd)</span>
<span class="sd">    :return:</span>
<span class="sd">        Return True if process matches</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># find the pid from by port</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;netstat -nlp | grep &#39;:</span><span class="si">%s</span><span class="s2"> &#39;|awk -F &#39; &#39; &#39;{print $7}&#39;|</span><span class="se">\</span>
<span class="s2">        cut -d </span><span class="se">\&quot;</span><span class="s2">/</span><span class="se">\&quot;</span><span class="s2"> -f1&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">port</span><span class="p">)</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
    <span class="k">if</span> <span class="mi">0</span> <span class="o">!=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]:</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="n">stdout</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="k">if</span> <span class="mi">0</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">stdout</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="n">dst_pid</span> <span class="o">=</span> <span class="n">stdout</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="c1"># check the path</span>
    <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">sel_path</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;exe&#39;</span><span class="p">,</span> <span class="s1">&#39;cwd&#39;</span><span class="p">]:</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;ls -l /proc/</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">|awk </span><span class="se">\&#39;</span><span class="s1">{print $11}</span><span class="se">\&#39;</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">dst_pid</span><span class="p">,</span> <span class="n">sel_path</span><span class="p">)</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
        <span class="n">pid_path</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="mi">0</span> <span class="o">==</span> <span class="n">pid_path</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span>


<span class="k">class</span> <span class="nc">Asynccontent</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    make a Argcontent to async_run u have to del it after using it</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cmd</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timeout</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pid</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ret</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;stdout&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s1">&#39;stderr&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s1">&#39;returncode&#39;</span><span class="p">:</span> <span class="mi">0</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">child_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cmdthd</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">monitorthd</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">subproc</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tempscript</span> <span class="o">=</span> <span class="kc">None</span>


<span class="k">class</span> <span class="nc">ShellExec</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>  <span class="c1"># pylint: disable=R0903</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    For shell command execution.</span>

<span class="sd">    ::</span>
<span class="sd">        from cup import shell</span>
<span class="sd">        shellexec = shell.ShellExec()</span>
<span class="sd">        # timeout=None will block the execution until it finishes</span>
<span class="sd">        shellexec.run(&#39;/bin/ls&#39;, timeout=None)</span>
<span class="sd">        # timeout&gt;=0 will open non-blocking mode</span>
<span class="sd">        # The process will be killed if the cmd timeouts</span>
<span class="sd">        shellexec.run(cmd=&#39;/bin/ls&#39;, timeout=100)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tmpdir</span><span class="o">=</span><span class="s1">&#39;/tmp/&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param tmpdir:</span>
<span class="sd">            shellexec will use tmpdir to handle temp files</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_subpro</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_subpro_data</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tmpdir</span> <span class="o">=</span> <span class="n">tmpdir</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tmpprefix</span> <span class="o">=</span> <span class="s1">&#39;cup.shell.</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">())</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">kill_all_process</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">async_content</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        to kill all process</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">async_content</span><span class="o">.</span><span class="n">child_list</span><span class="p">:</span>
            <span class="n">os</span><span class="o">.</span><span class="n">kill</span><span class="p">(</span><span class="n">pid</span><span class="p">,</span> <span class="n">signal</span><span class="o">.</span><span class="n">SIGKILL</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">which</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">pgm</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get executable&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">pgm</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">access</span><span class="p">(</span><span class="n">pgm</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">X_OK</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">pgm</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getenv</span><span class="p">(</span><span class="s1">&#39;PATH&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">fpath</span> <span class="ow">in</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">pathsep</span><span class="p">):</span>
            <span class="n">fpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">fpath</span><span class="p">,</span> <span class="n">pgm</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">fpath</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">access</span><span class="p">(</span><span class="n">fpath</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">X_OK</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">fpath</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">get_async_run_status</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">async_content</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get the process status of executing async cmd</span>

<span class="sd">        :return:</span>
<span class="sd">            None if the process has finished.</span>
<span class="sd">            Otherwise, return a object of linux.Process(async_pid)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">async_process</span> <span class="o">=</span> <span class="n">linux</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">async_content</span><span class="o">.</span><span class="n">pid</span><span class="p">)</span>
            <span class="n">res</span> <span class="o">=</span> <span class="n">async_process</span><span class="o">.</span><span class="n">get_process_status</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">err</span><span class="o">.</span><span class="n">NoSuchProcess</span><span class="p">:</span>
            <span class="n">res</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">res</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">get_async_run_res</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">async_content</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        if the process is still running the res shoule be None,None,0</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">async_content</span><span class="o">.</span><span class="n">ret</span>

    <span class="k">def</span> <span class="nf">async_run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        async_run</span>
<span class="sd">        return a dict {uuid:pid}</span>
<span class="sd">        self.argcontent{cmd,timeout,ret,cmdthd,montor}</span>
<span class="sd">        timeout:returncode:999</span>
<span class="sd">        cmd is running returncode:-999</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="nf">_signal_handle</span><span class="p">():</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            signal setup</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="n">signal</span><span class="o">.</span><span class="n">signal</span><span class="p">(</span><span class="n">signal</span><span class="o">.</span><span class="n">SIGPIPE</span><span class="p">,</span> <span class="n">signal</span><span class="o">.</span><span class="n">SIG_DFL</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">_target</span><span class="p">(</span><span class="n">argcontent</span><span class="p">,</span> <span class="n">proc_cond</span><span class="p">):</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">tempscript</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">NamedTemporaryFile</span><span class="p">(</span>
                <span class="nb">dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_tmpdir</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_tmpprefix</span><span class="p">,</span>
                <span class="n">delete</span><span class="o">=</span><span class="kc">True</span>
            <span class="p">)</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">argcontent</span><span class="o">.</span><span class="n">tempscript</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s1">&#39;w+b&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
                <span class="n">fhandle</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;cd </span><span class="si">{0}</span><span class="s1">;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()))</span>
                <span class="n">fhandle</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">argcontent</span><span class="o">.</span><span class="n">cmd</span><span class="p">)</span>
            <span class="n">shexe</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">which</span><span class="p">(</span><span class="s1">&#39;sh&#39;</span><span class="p">)</span>
            <span class="n">cmds</span> <span class="o">=</span> <span class="p">[</span><span class="n">shexe</span><span class="p">,</span> <span class="n">argcontent</span><span class="o">.</span><span class="n">tempscript</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="s1">&#39;to async execute </span><span class="si">{0}</span><span class="s1"> with script </span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">argcontent</span><span class="o">.</span><span class="n">cmd</span><span class="p">,</span> <span class="n">cmds</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">proc_cond</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
                <span class="n">argcontent</span><span class="o">.</span><span class="n">subproc</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span>
                        <span class="n">cmds</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
                        <span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
                        <span class="n">preexec_fn</span><span class="o">=</span><span class="n">_signal_handle</span><span class="p">)</span>
                <span class="n">proc_cond</span><span class="o">.</span><span class="n">notify</span><span class="p">()</span>
                <span class="n">proc_cond</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
                <span class="n">proc_cond</span><span class="o">.</span><span class="n">notify</span><span class="p">()</span>
                <span class="n">proc_cond</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
                <span class="n">argcontent</span><span class="o">.</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
                <span class="n">argcontent</span><span class="o">.</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stderr&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="s1">&#39;failed to execute the cmd, plz check it out</span><span class="se">\&#39;</span><span class="s1">s&#39;</span>
                <span class="p">)</span>

        <span class="k">def</span> <span class="nf">_monitor</span><span class="p">(</span><span class="n">start_time</span><span class="p">,</span> <span class="n">argcontent</span><span class="p">):</span>
            <span class="k">while</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()))</span> <span class="o">-</span> <span class="nb">int</span><span class="p">(</span><span class="n">start_time</span><span class="p">)</span> <span class="o">&lt;</span>
                    <span class="nb">int</span><span class="p">(</span><span class="n">argcontent</span><span class="o">.</span><span class="n">timeout</span><span class="p">)):</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">argcontent</span><span class="o">.</span><span class="n">subproc</span><span class="o">.</span><span class="n">poll</span><span class="p">()</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_subpro_data</span> <span class="o">=</span> <span class="n">argcontent</span><span class="o">.</span><span class="n">subproc</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
                    <span class="n">argcontent</span><span class="o">.</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">argcontent</span><span class="o">.</span><span class="n">subproc</span><span class="o">.</span><span class="n">returncode</span>
                    <span class="n">argcontent</span><span class="o">.</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subpro_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">argcontent</span><span class="o">.</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stderr&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subpro_data</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="k">return</span>
            <span class="n">parent</span> <span class="o">=</span> <span class="n">linux</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">argcontent</span><span class="o">.</span><span class="n">subproc</span><span class="o">.</span><span class="n">pid</span><span class="p">)</span>
            <span class="n">children</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">children</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">ret_dict</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">process</span> <span class="ow">in</span> <span class="n">children</span><span class="p">:</span>
                <span class="n">ret_dict</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">process</span><span class="p">)</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">child_list</span> <span class="o">=</span> <span class="n">ret_dict</span>
            <span class="n">str_warn</span> <span class="o">=</span> <span class="p">(</span>
                <span class="s1">&#39;Shell &quot;</span><span class="si">{0}</span><span class="s1">&quot;execution timout:</span><span class="si">{1}</span><span class="s1">. To kill it&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">argcontent</span><span class="o">.</span><span class="n">cmd</span><span class="p">,</span> <span class="n">argcontent</span><span class="o">.</span><span class="n">timeout</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">kill_all_process</span><span class="p">(</span><span class="n">argcontent</span><span class="p">)</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">999</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stderr&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">str_warn</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">subproc</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>

        <span class="n">argcontent</span> <span class="o">=</span> <span class="n">Asynccontent</span><span class="p">()</span>
        <span class="n">argcontent</span><span class="o">.</span><span class="n">cmd</span> <span class="o">=</span> <span class="n">cmd</span>
        <span class="n">argcontent</span><span class="o">.</span><span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span>
        <span class="n">argcontent</span><span class="o">.</span><span class="n">ret</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;stdout&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s1">&#39;stderr&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s1">&#39;returncode&#39;</span><span class="p">:</span> <span class="o">-</span><span class="mi">999</span>
        <span class="p">}</span>
        <span class="n">proc_cond</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Condition</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">())</span>
        <span class="n">argcontent</span><span class="o">.</span><span class="n">cmdthd</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span>
            <span class="n">target</span><span class="o">=</span><span class="n">_target</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">argcontent</span><span class="p">,</span> <span class="n">proc_cond</span><span class="p">))</span>
        <span class="n">argcontent</span><span class="o">.</span><span class="n">cmdthd</span><span class="o">.</span><span class="n">daemon</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">proc_cond</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
        <span class="n">argcontent</span><span class="o">.</span><span class="n">cmdthd</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="n">start_time</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()))</span>
        <span class="n">argcontent</span><span class="o">.</span><span class="n">cmdthd</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
        <span class="n">proc_cond</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
        <span class="n">proc_cond</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">argcontent</span><span class="o">.</span><span class="n">subproc</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">pid</span> <span class="o">=</span> <span class="n">argcontent</span><span class="o">.</span><span class="n">subproc</span><span class="o">.</span><span class="n">pid</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">monitorthd</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">_monitor</span><span class="p">,</span>
                    <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">start_time</span><span class="p">,</span> <span class="n">argcontent</span><span class="p">))</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">monitorthd</span><span class="o">.</span><span class="n">daemon</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">monitorthd</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
            <span class="c1">#this join should be del if i can make if quicker in Process.children</span>
            <span class="n">argcontent</span><span class="o">.</span><span class="n">cmdthd</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">argcontent</span>

    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        refer to the class description</span>

<span class="sd">        :param timeout:</span>
<span class="sd">            If the cmd is not returned after [timeout] seconds, the cmd process</span>
<span class="sd">            will be killed. If timeout is None, will block there until the cmd</span>
<span class="sd">            execution returns</span>

<span class="sd">        :return:</span>
<span class="sd">            {</span>
<span class="sd">                &#39;stdout&#39; : &#39;Success&#39;,</span>
<span class="sd">                &#39;stderr&#39; : None,</span>
<span class="sd">                &#39;returncode&#39; : 0</span>
<span class="sd">            }</span>
<span class="sd">            returncode == 0 means success, while 999 means timeout</span>

<span class="sd">        E.g.</span>

<span class="sd">        ::</span>
<span class="sd">            import cup</span>
<span class="sd">            shelltool = cup.shell.ShellExec()</span>
<span class="sd">            print shelltool.run(&#39;/bin/ls&#39;, timeout=1)</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">def</span> <span class="nf">_signal_handle</span><span class="p">():</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            signal setup</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="n">signal</span><span class="o">.</span><span class="n">signal</span><span class="p">(</span><span class="n">signal</span><span class="o">.</span><span class="n">SIGPIPE</span><span class="p">,</span> <span class="n">signal</span><span class="o">.</span><span class="n">SIG_DFL</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">_trans_bytes</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;trans bytes into unicode for python3&quot;&quot;&quot;</span>
            <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_py2</span><span class="p">():</span>
                <span class="k">return</span> <span class="n">data</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">data</span> <span class="o">=</span> <span class="nb">bytes</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                    <span class="n">data</span> <span class="o">=</span> <span class="s1">&#39;Error to decode result&#39;</span>
            <span class="k">return</span> <span class="n">data</span>

        <span class="k">def</span> <span class="nf">_pipe_asshell</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            run shell with subprocess.Popen</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="n">tempscript</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">NamedTemporaryFile</span><span class="p">(</span>

                <span class="nb">dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_tmpdir</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_tmpprefix</span><span class="p">,</span>
                <span class="n">delete</span><span class="o">=</span><span class="kc">True</span>
            <span class="p">)</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">tempscript</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s1">&#39;w+&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
                <span class="n">fhandle</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;cd </span><span class="si">{0}</span><span class="s1">;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()))</span>
                <span class="n">fhandle</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
            <span class="n">shexe</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">which</span><span class="p">(</span><span class="s1">&#39;sh&#39;</span><span class="p">)</span>
            <span class="n">cmds</span> <span class="o">=</span> <span class="p">[</span><span class="n">shexe</span><span class="p">,</span> <span class="n">tempscript</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="s1">&#39;cup shell execute </span><span class="si">{0}</span><span class="s1"> with script </span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">cmd</span><span class="p">,</span> <span class="n">cmds</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_subpro</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span>
                <span class="n">cmds</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
                <span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">preexec_fn</span><span class="o">=</span><span class="n">_signal_handle</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_subpro_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subpro</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;stdout&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s1">&#39;stderr&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s1">&#39;returncode&#39;</span><span class="p">:</span> <span class="mi">0</span>
        <span class="p">}</span>
        <span class="n">cmdthd</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span>
            <span class="n">target</span><span class="o">=</span><span class="n">_pipe_asshell</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="p">)</span>
        <span class="p">)</span>
        <span class="n">cmdthd</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="n">cmdthd</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">timeout</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">cmdthd</span><span class="o">.</span><span class="n">isAlive</span><span class="p">():</span>
            <span class="n">str_warn</span> <span class="o">=</span> <span class="p">(</span>
                <span class="s1">&#39;Shell &quot;</span><span class="si">%s</span><span class="s1">&quot;execution timout:</span><span class="si">%d</span><span class="s1">. Killed it&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">str_warn</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>
            <span class="n">parent</span> <span class="o">=</span> <span class="n">linux</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_subpro</span><span class="o">.</span><span class="n">pid</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">parent</span><span class="o">.</span><span class="n">children</span><span class="p">(</span><span class="kc">True</span><span class="p">):</span>
                <span class="n">os</span><span class="o">.</span><span class="n">kill</span><span class="p">(</span><span class="n">child</span><span class="p">,</span> <span class="n">signal</span><span class="o">.</span><span class="n">SIGKILL</span><span class="p">)</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">999</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stderr&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">str_warn</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_subpro</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_subpro</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
            <span class="n">times</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subpro</span><span class="o">.</span><span class="n">returncode</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">times</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">:</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="n">times</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subpro</span><span class="o">.</span><span class="n">returncode</span>
            <span class="k">assert</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_subpro_data</span><span class="p">)</span> <span class="o">==</span> <span class="nb">tuple</span><span class="p">,</span> \
                <span class="s1">&#39;self._subpro_data should be a tuple&#39;</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">_trans_bytes</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_subpro_data</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stderr&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">_trans_bytes</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_subpro_data</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">ret</span>


<span class="k">def</span> <span class="nf">_do_execshell</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">b_printcmd</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    do execshell</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">timeout</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">timeout</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">ShellException</span><span class="p">(</span>
            <span class="s1">&#39;timeout should be None or &gt;= 0&#39;</span>
        <span class="p">)</span>
    <span class="k">if</span> <span class="n">b_printcmd</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;To exec cmd:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">cmd</span><span class="p">))</span>
    <span class="n">shellexec</span> <span class="o">=</span> <span class="n">ShellExec</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">shellexec</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">execshell</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">b_printcmd</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    执行shell命令，返回returncode</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_do_execshell</span><span class="p">(</span>
        <span class="n">cmd</span><span class="p">,</span> <span class="n">b_printcmd</span><span class="o">=</span><span class="n">b_printcmd</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">)[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span>


<span class="k">def</span> <span class="nf">execshell_withpipe</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Deprecated. Use ShellExec instead</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">res</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">res</span>


<span class="k">def</span> <span class="nf">execshell_withpipe_ex</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">b_printcmd</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Deprecated. Recommand using ShellExec.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">strfile</span> <span class="o">=</span> <span class="s1">&#39;/tmp/</span><span class="si">%s</span><span class="s1">.</span><span class="si">%d</span><span class="s1">.</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="s1">&#39;shell_env.py&#39;</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()),</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">100000</span><span class="p">,</span> <span class="mi">999999</span><span class="p">)</span>
    <span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">mknod</span><span class="p">(</span><span class="n">strfile</span><span class="p">)</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="n">cmd</span> <span class="o">+</span> <span class="s1">&#39; 1&gt;&#39;</span> <span class="o">+</span> <span class="n">strfile</span> <span class="o">+</span> <span class="s1">&#39; 2&gt;/dev/null&#39;</span>
    <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
    <span class="k">if</span> <span class="kc">True</span> <span class="o">==</span> <span class="n">b_printcmd</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
    <span class="n">fphandle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">strfile</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="n">lines</span> <span class="o">=</span> <span class="n">fphandle</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
    <span class="n">fphandle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">strfile</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">lines</span>


<span class="k">def</span> <span class="nf">execshell_withpipe_str</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">b_printcmd</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Deprecated. Recommand using ShellExec.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">execshell_withpipe_ex</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">b_printcmd</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">execshell_withpipe_exwitherr</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">b_printcmd</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Deprecated. Recommand using ShellExec.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">strfile</span> <span class="o">=</span> <span class="s1">&#39;/tmp/</span><span class="si">%s</span><span class="s1">.</span><span class="si">%d</span><span class="s1">.</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="s1">&#39;shell_env.py&#39;</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()),</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">100000</span><span class="p">,</span> <span class="mi">999999</span><span class="p">)</span>
    <span class="p">)</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="n">cmd</span> <span class="o">+</span> <span class="s1">&#39; &gt;&#39;</span> <span class="o">+</span> <span class="n">strfile</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="n">cmd</span> <span class="o">+</span> <span class="s1">&#39; 2&gt;&amp;1&#39;</span>
    <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">b_printcmd</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
    <span class="n">fhandle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">strfile</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="n">lines</span> <span class="o">=</span> <span class="n">fhandle</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
    <span class="n">fhandle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">strfile</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">lines</span>


<span class="k">def</span> <span class="nf">is_proc_alive</span><span class="p">(</span><span class="n">procname</span><span class="p">,</span> <span class="n">is_whole_word</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">is_server_tag</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">filters</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Deprecated. Recommand using cup.oper.is_proc_exist</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># print procName</span>
    <span class="k">if</span> <span class="n">is_whole_word</span><span class="p">:</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;ps -ef|grep -w &#39;</span><span class="si">%s</span><span class="s2">&#39;$ |grep -v grep&quot;</span> <span class="o">%</span> <span class="n">procname</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;ps -ef|grep -w &#39;</span><span class="si">%s</span><span class="s2">&#39; |grep -v grep&quot;</span> <span class="o">%</span> <span class="n">procname</span>

    <span class="k">if</span> <span class="n">is_server_tag</span><span class="p">:</span>
        <span class="n">cmd</span> <span class="o">+=</span> <span class="s1">&#39;|grep -vwE &quot;vim |less |vi |tail |cat |more &quot;&#39;</span>
    <span class="k">if</span> <span class="n">filters</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filters</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">cmd</span> <span class="o">+=</span> <span class="s2">&quot;|grep -v &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="n">filters</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filters</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">task</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">filters</span><span class="p">):</span>
                <span class="n">cmd</span> <span class="o">+=</span> <span class="s2">&quot;|grep -v &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="n">task</span>
    <span class="n">cmd</span> <span class="o">+=</span> <span class="s1">&#39;|wc -l&#39;</span>
    <span class="n">rev</span> <span class="o">=</span> <span class="n">execshell_withpipe_str</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">rev</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>


<span class="k">def</span> <span class="nf">forkexe_shell</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    fork a new process to execute cmd (os.system(cmd))</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">pid</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span>
    <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="c1"># os.chdir(&quot;/&quot;)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">setsid</span><span class="p">()</span>
    <span class="c1"># os.umask(0)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">pid</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">md5file</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    compute md5 hex value of a file, return with a string (hex-value)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s1">&#39;No such file: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">filename</span><span class="p">)</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
        <span class="n">md5obj</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">md5</span><span class="p">()</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">strtmp</span> <span class="o">=</span> <span class="n">fhandle</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">131072</span><span class="p">)</span>  <span class="c1"># read 128k one time</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">strtmp</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">break</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">strtmp</span><span class="p">,</span> <span class="n">unicode</span><span class="p">):</span>
                <span class="n">md5obj</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">strtmp</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">md5obj</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">strtmp</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">md5obj</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">kill9_byname</span><span class="p">(</span><span class="n">strname</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    kill -9 process by name</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fd_pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s2">&quot;ps -ef | grep -v grep |grep </span><span class="si">%s</span><span class="s2"> </span><span class="se">\</span>
<span class="s2">            |awk &#39;{print $2}&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">strname</span><span class="p">))</span>
    <span class="n">pids</span> <span class="o">=</span> <span class="n">fd_pid</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">fd_pid</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">pids</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s2">&quot;kill -9 </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pid</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">kill_byname</span><span class="p">(</span><span class="n">strname</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    kill process by name</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fd_pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s2">&quot;ps -ef | grep -v grep |grep </span><span class="si">%s</span><span class="s2"> </span><span class="se">\</span>
<span class="s2">            |awk &#39;{print $2}&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">strname</span><span class="p">))</span>
    <span class="n">pids</span> <span class="o">=</span> <span class="n">fd_pid</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">fd_pid</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">pids</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s2">&quot;kill -s SIGKILL </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pid</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">del_if_exist</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">safemode</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    delete the path if it exists, cannot delete root / under safemode</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">safemode</span> <span class="ow">and</span> <span class="n">path</span> <span class="o">==</span> <span class="s1">&#39;/&#39;</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s1">&#39;Cannot delete root path /&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">lexists</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
        <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="ow">or</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">islink</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s1">&#39;Does not support deleting the type 4 the path&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">rmtree</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">ignore_errors</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">onerror</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">safemode</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    safe rmtree.</span>

<span class="sd">    safemode, by default is True, which forbids:</span>

<span class="sd">    1. not allowing rmtree root &quot;/&quot;</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">safemode</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">path</span><span class="p">))</span> <span class="o">==</span> <span class="s1">&#39;/&#39;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">ShellException</span><span class="p">(</span><span class="s1">&#39;cannot rmtree root / under safemode&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">ignore_errors</span><span class="p">,</span> <span class="n">onerror</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">shell_diff</span><span class="p">(</span><span class="n">srcfile</span><span class="p">,</span> <span class="n">dstfile</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    shell diff two files, return 0 if it&#39;s the same.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;diff </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">srcfile</span><span class="p">,</span> <span class="n">dstfile</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">get_pid</span><span class="p">(</span><span class="n">process_path</span><span class="p">,</span> <span class="n">grep_string</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    will return immediately after find the pid which matches</span>

<span class="sd">    1. ps -ef|grep %s|grep -v grep|grep -vE &quot;^[vim|less|vi|tail|cat|more] &quot;</span>
<span class="sd">    &#39;|awk &#39;{print $2}&#39;</span>

<span class="sd">    2. workdir is the same as ${process_path}</span>

<span class="sd">    :param process_path:</span>
<span class="sd">        process that runs on</span>
<span class="sd">    :param grep_string:</span>
<span class="sd">        ps -ef|grep ${grep_string}</span>
<span class="sd">    :return:</span>
<span class="sd">        return None if not found. Otherwise, return the pid</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">cmd</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s1">&#39;ps -ef|grep </span><span class="se">\&#39;</span><span class="si">%s</span><span class="se">\&#39;</span><span class="s1">|grep -v grep|grep -vwE &quot;vim |less |vi |tail |cat |more &quot;&#39;</span>
        <span class="s1">&#39;|awk </span><span class="se">\&#39;</span><span class="s1">{print $2}</span><span class="se">\&#39;</span><span class="s1">&#39;</span>
    <span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">grep_string</span><span class="p">)</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
    <span class="n">pids</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">pids</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">pids</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">pids</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>
    <span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">pids</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">sel_path</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;cwd&quot;</span><span class="p">,</span> <span class="s2">&quot;exe&quot;</span><span class="p">]:</span>
            <span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;ls -l /proc/</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">|awk </span><span class="se">\&#39;</span><span class="s1">{print $11}</span><span class="se">\&#39;</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pid</span><span class="p">,</span> <span class="n">sel_path</span><span class="p">)</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
            <span class="n">pid_path</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">pid_path</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">pid</span>
    <span class="k">return</span> <span class="kc">None</span>

<span class="c1"># end linux functionalities }}</span>
<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'3.2.19',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../../_static/language_data.js"></script>

  

  <script type="text/javascript" src="../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>